﻿var GetAttchListUrl = GetRootPath() + "frame/base/attach/attachlist"
var UploadAttachUrl = GetRootPath() + "frame/base/attach/upload"
var DeleteAttachUrl = GetRootPath() + "frame/base/attach/delete"
var ReadAttachUrl = GetRootPath() + "frame/base/attach/readfile"
$(function () {
    var cloudUploadList = $("cloud-upload");
    for (var i = 0; i < cloudUploadList.length; i++) {
        var cloudUpload = cloudUploadList[i];
        var id = $(cloudUpload).attr("id");
        if (!id) {
            id = GetNewGuid();
        }
        var cloudUploadtBtnID = "cloudUploadBtnID_" + id;
        var cloudUploadFileID = "cloudUploadFileID_" + id;
        var cloudUploadTipID = "cloudUploadTipID_" + id;
        var cloudUploaFileListID = "cloudUploaFileListID_" + id;
        var GroupGuid = $(cloudUpload).attr("groupguid");
        var GroupType = $(cloudUpload).attr("grouptype");
        var text = "";
        text += "  <div id=\"fileupload\" class=\"cloud-upload-container\">";
        text += "            <button class=\"btn btn-primary cloudbtn uploadbtn\" type=\"button\" id=" + cloudUploadtBtnID + "><span class=\"btntext\"><i class=\"icon icon-upload\"></i><span class=\"searchtext\">选择文件</span></span></button>";
        text += "            <input style=\"display:none\"  class='uploadfile' type=\"file\" id=" + cloudUploadFileID + " groupguid='" + GroupGuid + "' grouptype='" + GroupType + "' />";
        text += "            <div class=\"cloud-upload-errorinfo\" id=" + cloudUploadTipID + " style='display:none'>";
        text += "                <span style=\"font-size:14px;color:red\"></span>";
        text += "            </div>";
        text += "            <div class=\"cloud-upload-filelist\" id=" + cloudUploaFileListID + ">";
        text += "            </div>";
        text += "        </div>";
        cloudUpload.innerHTML = text;
        BindCloudAttach(GroupGuid, GroupType, cloudUploaFileListID);
    }
    $(".uploadbtn").each(function () {
        $(this).click(function () {
            var cloudUploadFileID = "cloudUploadFileID_" + $(this).attr("id").split("_")[1];
            $("#" + cloudUploadFileID).click();
        })
    })
    $(".uploadfile").each(function () {
        var GroupGuid = $(this).attr("groupguid");
        var GroupType = $(this).attr("grouptype");
        $(this).change(function () {
            var cloudUploadFileID = "cloudUploadFileID_" + $(this).attr("id").split("_")[1];
            var cloudUploaFileListID = "cloudUploaFileListID_" + $(this).attr("id").split("_")[1];
            var fileUpload = $("#" + cloudUploadFileID).get(0);
            var files = fileUpload.files;
            var formData = new FormData();
            for (var i = 0; i < files.length; i++) {
                formData.append(files[i].name, files[i]);
            }
            formData.append("GroupGuid", GroupGuid);
            formData.append("GroupType", GroupType);
            CloudPostFile(formData, UploadAttachUrl, function (res) {
                if (res.code == 0) {
                    BindCloudAttach(GroupGuid, GroupType, cloudUploaFileListID);
                } else {
                    OpenFail(res.data);
                }
            })
            $("#" + cloudUploadFileID).val('');
        })
    })
})

function BindCloudAttach(GroupGuid, GroupType, FileListID) {
    if (GroupGuid != "") {
        var param = { "GroupGuid": GroupGuid, "GroupType": GroupType }
        CloudPost(param, GetAttchListUrl, function (res) {
            if (res.code == 0) {
                var text = "";
                $(res.data).each(function () {
                    text += "                <div class=\"cloud-upload-fileitem\">";
                    text += "                    <i style=\"font-size:14px;cursor:pointer;margin-right:5px\" class=\"icon  icon-file-text-o\"></i>";
                    text += "                    <span style=\"font-size:14px;cursor:pointer\" onclick=ReadCloudAttach('" + this.AttachGuid + "')>" + this.AttachFileName + "</span>";
                    text += "                    <i style=\"font-size:14px;cursor:pointer;margin-left:10px\" class=\"icon icon-remove-sign\"  onclick=DeleteCloudAttach('" + this.AttachGuid + "','" + GroupGuid + "','" + GroupType + "','" + FileListID + "')></i>";
                    text += "                </div>";
                })
                $("#" + FileListID).html(text);
            }
        })
    }

}

function DeleteCloudAttach(AttachGuid, GroupGuid, GroupType, FileListID) {
    var param = { "AttachGuid": AttachGuid }
    CloudPost(param, DeleteAttachUrl, function (res) {
        if (res.code == 0) {
            BindCloudAttach(GroupGuid, GroupType, FileListID);
        }
    })
}

function ReadCloudAttach(AttachGuid) {
    window.top.open(ReadAttachUrl + "?AttachGuid=" + AttachGuid + "&Token=" + GetLocalData("Token"));
}

function GetRootPath() {
    var loc = window.location,
        host = loc.hostname,
        protocol = loc.protocol,
        port = loc.port ? (':' + loc.port) : '';
    var path = location.pathname;

    if (path.indexOf('/') === 0) {
        path = path.substring(1);
    }

    var mypath = '/' + path.split('/')[0];
    path = (mypath != undefined ? mypath : ('/' + loc.pathname.split('/')[1])) + '/';

    var rootPath = protocol + '//' + host + port + path;
    return rootPath;
}